<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{departname}}</block>
		</cu-custom>

	<!-- 	<view class="cu-bar bg-white solid-bottom margin-top">
			<view class="action">
				<text class="cuIcon-title text-orange "></text>{{departname}}
			</view>
		</view> -->
		<view v-if="departmentlist.length>0" class="cu-list sm-border menu margin-top">
			<view @click="DepartClick" class="cu-item arrow" v-for="(depart,index) in departmentlist" :key="index" :data-id="depart.org_code" :data-name="depart.departname">
				<text class="text-grey">{{depart.departname}}({{depart.cou}}人)</text>
			</view>
		</view>
	

		<view class="cu-list menu-avatar margin-top">
			<view @click="EmployeeDetail" class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''" v-for="(item,index) in employeelist"
			:data-id="item.id" :key="index" @touchstart="ListTouchStart" @touchmove="ListTouchMove" @touchend="ListTouchEnd" :data-target="'move-box-' + index">
				<view class="cu-avatar round lg" :style="[{backgroundImage:'url('+ ($constant.IMG_VERSION == $constant.OSS?item.personphoto:$constant.BASEURL+item.personphoto) +')'}]"></view>
				<view class="content">
					<view class="text-grey">{{item.personname}}</view>
					<view class="text-gray text-sm">
						{{item.staffno}}
					</view>
				</view>
				<view class="action">
					<!-- <view class="text-grey text-xs">22:20</view>
					<view class="cu-tag round bg-grey sm">5</view> -->
				</view>
				<view class="move">
					<!-- <view class="bg-grey">置顶</view> -->
					<view class="bg-red" @click.stop="showDeleteDialog(index,item.id)">删除</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" :size="20" :content-text="contentText" />
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	let globalData = getApp().globalData
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				modalName: null,
				employeelist: [],
				departmentlist:[],
				departname:'员工列表',
				orgcode:'',
				page: 1,
				rows: 10,
				reload: false, //是否重新加载
				status: 'more',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
				}
			}
		},
		onLoad(options) {
			if(typeof(options.orgcode)!='undefined'){
				this.orgcode = options.orgcode
			}else{
				this.orgcode = globalData.orgcode
			}
			if(typeof(options.departname)!='undefined'){
				this.departname = options.departname
			}
			this.getDepart()
			this.getEmployee()
		},
		onShow() {
			console.log(this.orgcode)
		},
		onReachBottom() {
			if (this.status == 'noMore') return
			this.status = 'loading';
			this.getEmployee()
		},
		methods: {
			DepartClick(e){
				let orgcode = e.currentTarget.dataset.id;
				let name = e.currentTarget.dataset.name;
				uni.navigateTo({
					url:'employeelist?orgcode='+orgcode+'&departname='+name
				})
			},
			EmployeeDetail(e) {
				let id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: 'change-employee?id='+id
				})
			},
			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
				this.listTouchStartY = e.touches[0].pageY
			},

			// ListTouch计算方向
			ListTouchMove(e) {
				var offsetX = e.touches[0].pageX - this.listTouchStart
				var offsetY = e.touches[0].pageY - this.listTouchStartY
				// console.log(offsetY)
				if (offsetY < 20 && offsetY > -20) {
					if (offsetX > 50) {
						this.listTouchDirection = 'right'
					}
					if (offsetX < -50) {
						this.listTouchDirection = 'left'
					}
				}
			},
			// ListTouch计算滚动
			ListTouchEnd(e) {
				// console.log(this.listTouchDirection)
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			},
			getDepart(){
				//说明已有数据，目前处于上拉加载
				uni.request({
					url: this.$constant.BASEURL + this.$constant.GET_DEPARTMENT_STATITICS,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: globalData.username,
						unitno: this.orgcode,
						token: globalData.token,
					},
					success: (res) => {
						console.log(res.data)
						if(res.data.code == 0){
							let list = res.data.data
							if(typeof(list)!='undefined'){
								this.departmentlist = res.data.data
							}
						}
					}
				});
			},
			getEmployee() {
				//说明已有数据，目前处于上拉加载
				console.log(this.orgcode)
				uni.request({
					url: this.$constant.BASEURL + this.$constant.GET_EMPLOYEE_LIST_PAGE,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: globalData.username,
						unitnos: this.orgcode,
						token: globalData.token,
						page:this.page,
						rows:this.rows
					},
					success: (res) => {
						let page = res.data.data
						console.log(page)
						if(typeof(page)=='undefined') return
						console.log(page)
						if (page.page * page.rows >= page.total) {
							this.status = 'noMore';
						}
						if (res.data.code == 0) {
							let list = page.results
							this.employeelist = this.reload ? list : this.employeelist.concat(list)
							this.page++;
							this.reload = false;
							// console.log(list)
						} else {
							uni.showModal({
								content: res.data.message,
								showCancel: false
							})
						}
					}
				});
			},
			showDeleteDialog(index, employeeid) {
				let that = this
				uni.showModal({
					title: '删除人员',
					content: '确定要删除该员工吗？',
					success(res) {
						if(res.confirm){
							that.deleteEmployee(index, employeeid)
						}
					}
				})
			},
			deleteEmployee(index, employeeid) {
				// console.log(employeeid)
				if (typeof(employeeid) == 'undefined' || employeeid == '') {
					return
				}
				uni.request({
					url: this.$constant.BASEURL + this.$constant.DELETE_EMPLOYEE_BY_ID,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: globalData.username,
						id: employeeid,
						token: globalData.token
					},
					success: (res) => {
						var result = res.data
						// console.log(result)
						if (result.code == 0) {
							uni.showToast({
								title: '删除成功',
							})
							this.employeelist.splice(index, 1)
						} else {
							uni.showToast({
								title: result.message,
								icon: 'none'
							})
						}
					}
				});
			},
		}
	}
</script>

<style>

</style>
